package com.yangwp.doutu.controller;

import com.yangwp.doutu.Utils.ConvertUtil;
import com.yangwp.doutu.Utils.FileUtils;
import com.yangwp.doutu.Utils.MapUtil;
import com.yangwp.doutu.enums.RespEnum;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.helpers.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/head")
public class HeadController {

    @RequestMapping("/origin")
    @ResponseBody
    public void getOriginHead(HttpServletResponse response, String fileName){
        if(StringUtils.isBlank(fileName)){
            return;
        }
        response.setContentType("image/jpeg");
        File file = new File(FileUtils.ORIGIN_HEAD_PATH, fileName);
        if(file.exists()){
            try {
                OutputStream out = response.getOutputStream();
                InputStream in = new FileInputStream(file);
                IOUtils.copy(in, out);
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @ResponseBody
    @RequestMapping("/origin/list")
    public Map<String, Object> getOriginHeadList(){
        List<String> heads = FileUtils.getOriginHeadList();
        Map<String, Object> resp = MapUtil.getMap(RespEnum.OK);
        resp.put("data", heads);
        return resp;
    }

}
